﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using LEDHelper;
using ExcelX.ExcelLib;
using System.Windows.Forms;
namespace LEDDataBase
{
    public partial class LEDData
    {
        public LEDDataSet LEDDatas = new LEDDataSet();
        ExcelAgent exAgent;
        public LEDData()
        {

        }
        ~LEDData()
        {
            if (exAgent != null)
            {
                exAgent.Close();
            }
        }
        public void Reset()
        {

            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Data\LEDManagement.XML");
                if (!System.IO.File.Exists(filePath))
                {
                    return;
                }
                LEDDatas.Clear();
                setResult.ReadXml(filePath);
                LEDDatas.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Save()
        {
            SaveLocalData(LEDDatas);
        }
        public DataSet ResetLedData()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet settemp;
            try
            {


                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Data\LEDManagement.xls");
                if (!System.IO.File.Exists(filePath))
                {
                    return setReturn;
                }
                if (exAgent == null)
                {
                    exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
                }
                setResult = exAgent.LoadDataTableDef(filePath, "");
                filePath = Path.Combine(PathHelper.ApplicationPath, @"Data\LEDManagement.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                setResult.WriteXml(filePath);
                filePath = Path.Combine(PathHelper.ApplicationPath, @"Data\LEDManage.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                setResult.WriteXml(filePath);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                exAgent.Close();
                exAgent.Dispose();
            }
            return setReturn;
        }
        public DataSet LoadLedData()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Data\LEDManage.XML");
                //MessageBox.Show("LEDManage");
                if (!System.IO.File.Exists(filePath))
                {
                    filePath = Path.Combine(PathHelper.ApplicationPath, @"Data\LEDManagement.XML");
                    if (!System.IO.File.Exists(filePath))
                    {
                        return setReturn;
                    }
                }
                setResult.ReadXml(filePath);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                throw;
            }
            return setReturn;
        }
        public void SaveLocalData(DataSet setData)
        {
            try
            {
                setData.AcceptChanges();
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Data\LEDManagement.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                setData.WriteXml(filePath);
                filePath = Path.Combine(PathHelper.ApplicationPath, @"Data\LEDManage.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                setData.WriteXml(filePath);
            }
            catch (Exception ex)
            {
            }
        }
        public void AddLED(LEDDataSet setLED, Dictionary<string, string> LEDPara)
        {
            DataTable tbLED = setLED.LEDLIST;
            bool isnew = false;
            string nLEDID = LEDPara["LEDID"].ToString();
            DataRow dwLED = null;
            var query = from row in tbLED.AsEnumerable()
                        where row["LLLEDID"].ToString() == nLEDID
                        select row;
            foreach (DataRow row in query)
            {
                dwLED = row;                
            }
            if (dwLED == null)
            {
                isnew = true;
                dwLED = tbLED.NewRow();
            }
            dwLED["ROWID"] = 1;
            dwLED["LLSYSTEM"] = "000";
            dwLED["LLLEDID"] = nLEDID;
            dwLED["LLLEDPRE"] = LEDPara["LLLEDPRE"].ToString();
            dwLED["LLLEDNUMBER"] = LEDPara["LLLEDNUMBER"].ToString();
            dwLED["IPADDRESS"] = LEDPara["IPADDRESS"].ToString();
            dwLED["RMTHOST"] = LEDPara["RMTHOST"].ToString();
            dwLED["RMTPORT"] = LEDPara["RMTPORT"].ToString();
            dwLED["LOCPORT"] = LEDPara["LOCPORT"].ToString();
            dwLED["COMPORT"] = LEDPara["COMPORT"].ToString();
            dwLED["COMFLOW"] = "0";
            dwLED["DSTADDR"] = LEDPara["DSTADDR"].ToString();
            dwLED["DEVTYPE"] = LEDPara["DEVTYPE"].ToString();
            dwLED["COMSPEED"] = LEDPara["COMSPEED"].ToString();
            dwLED["TXTIMEO"] = 3;
            dwLED["TXREPEAT"] =2;
            dwLED["TXMOVEWIN"] =0;
            dwLED["PKPLENGTH"] =256;
            dwLED["KEY"] =110; 
            dwLED["DSPEED"] = LEDPara["DSPEED"].ToString();
            dwLED["LOCALPORT"] = LEDPara["LOCALPORT"].ToString();
            dwLED["WIDTH"] = LEDPara["WIDTH"].ToString();
            dwLED["HEIGHT"] = LEDPara["HEIGHT"].ToString();
            dwLED["NEWDATE"] = LEDPara["NEWDATE"].ToString();
            dwLED["NEWUSER"] = LEDPara["NEWUSER"].ToString();
            dwLED["LASTDATE"] = LEDPara["LASTDATE"].ToString();
            dwLED["LASTUSER"] = LEDPara["LASTUSER"].ToString();
            if (isnew)
            {
                tbLED.Rows.Add(dwLED);
            }
            SaveLocalData(setLED);
        }
        public string Right(string src, int bit)
        {
            string sout = "";
            int len = src.Length;

            sout = src.Substring(len - bit, bit);
            return sout;
        }
    }
}
